<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.userId" placeholder="用户ID" class="filter-item" clearable style="width: 200px;" />
      <el-input v-model="listQuery.userName" placeholder="用户名" class="filter-item" clearable style="width: 200px;" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      <el-button v-if="type == 0" v-waves class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">新增用户</el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="用户ID" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.userId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="权限" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.roleName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.createTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后登录IP" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.ip }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后登录时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.updateTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="type == 0" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{ row }">
          <el-button size="mini" icon="el-icon-setting" type="success" @click="handleReset(row)">重置密码</el-button>
          <el-button size="mini" icon="el-icon-edit" type="primary" @click="handleEdit(row)">编辑</el-button>
          <el-button size="mini" icon="el-icon-delete" type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
    <div v-if="dialogAddFormVisible">
      <el-dialog title="新增用户" :visible.sync="dialogAddFormVisible" :append-to-body="true" :close-on-click-modal="false" :destroy-on-close="true">
        <UserAddForm ref="userAddForm" :length="total" @close="userAddFormClose" @cancelClose="cancelClose(arguments)" />
      </el-dialog>
    </div>
    <div v-if="dialogEditFormVisible">
      <el-drawer :visible.sync="dialogEditFormVisible" direction="rtl" size="85%" :append-to-body="true" :destroy-on-close="true" :wrapper-closable="false" :with-header="false">
        <UserAddForm :id="editId" ref="userEditForm" @cancelClose="cancelClose(arguments)" />
      </el-drawer>
    </div>
  </div>
</template>

<script>
import { getSysUser, deleteSysUser, resetPassword } from '@/api/user';
import waves from '@/directive/waves';
import Pagination from '@/components/Pagination';
import UserAddForm from '@/views/sys/userAdd.vue';

export default {
  name: 'SysUser',
  components: { Pagination, UserAddForm },
  directives: { waves },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        userId: '',
        userName: ''
      },
      dialogAddFormVisible: false,
      dialogEditFormVisible: false,
      editId: 0,
      type: null
    };
  },
  created() {
    const { params } = this.$route
    Object.keys(params).forEach(key => {
      this.type = params[key].split(':')[1];
    });
    this.getList();
  },
  methods: {
    async getList() {
      this.listLoading = true;
      await getSysUser(this.listQuery).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    async handleSearch() {
      this.getList();
    },
    handleCreate() {
      this.dialogAddFormVisible = true;
    },
    async handleDelete(row) {
      await this.$confirm('确定要此条数据吗？', '刪除操作', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          this.listLoading = true;
          await deleteSysUser(row.id).then(res => {
            this.listLoading = false;
            if (res.msg !== 'OK') {
              this.$message.error(res.msg);
              return;
            }
            this.getList();
            this.$message.success('删除成功');
          });
        })
        .catch(() => {});
    },
    async handleReset(row) {
      await this.$confirm('确定要重置此用户密码吗？', '重置密码', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          this.listLoading = true;
          await resetPassword(row.id).then(res => {
            this.listLoading = false;
            if (res.msg !== 'OK') {
              this.$message.error(res.msg);
              return;
            }
            this.getList();
            this.$message.success('重置密码成功');
          });
        })
        .catch(() => {});
    },
    handleEdit(row) {
      this.editId = row.id;
      this.dialogEditFormVisible = true;
    },
    userAddFormClose() {
      this.dialogAddFormVisible = false;
    },
    cancelClose(data) {
      if (data[1] === 'add') {
        this.dialogAddFormVisible = false;
      } else {
        this.dialogEditFormVisible = false;
      }
      if (data[0]) {
        if (data[1] === 'add') {
          this.listQuery.pageNum = 1;
          this.listQuery.pageSize = 10;
          this.listQuery.userId = '';
          this.listQuery.userName = '';
        }
        this.getList();
      }
    }
  }
};
</script>
